using System.Collections;
using Holoville.HOTween;
using UnityEngine;

public class PointerPanel : MonoBehaviour
{
	private UIRoot uiRoot;

	public Transform target;

	public Vector3 targetVector;

	public Transform arrowObject;

	public UISprite arrowSprite;

	public float arrowSize;

	public Transform iconObject;

	public UISprite iconSprite;

	public Transform floatingObject;

	public UILabel distanceLabel;

	public float liveTime;

	public bool drawIconOnVisibleTarget = true;

	public bool drawArrow = true;

	public bool showDistance = true;

	private Vector3 newArrowObjPosition;

	private Vector3 newIconObjPosition;

	private Quaternion arrowRot;

	private Quaternion correctionRot;

	private Quaternion newRorarion;

	private PointerController pointerController;

	public float maxIconPosCorrection = 60f;

	[HideInInspector]
	public bool pointToVector;

	private Camera mainCamera;

	private Vector3 screenCenter = new Vector3((float)Screen.width / 2f, (float)Screen.height / 2f, 0f);

	private float windowSizeX;

	private float windowSizeY;

	private float leftBorderX;

	private float rightBorderX;

	private float topBorderY;

	private float bottomBorderY;

	private bool betweenHorizontalBorders;

	private bool betweenVerticalBorders;

	private float distance;

	private bool removing;

	private void Start()
	{
		pointerController = PointerController.instance;
		uiRoot = pointerController.uiRoot;
		mainCamera = pointerController.mainCamera;
		arrowRot = arrowSprite.transform.rotation;
		correctionRot = arrowRot;
		if (liveTime > 0f)
		{
			StartCoroutine(RemoveInTime(liveTime));
		}
		if (!showDistance)
		{
			distanceLabel.text = string.Empty;
		}
		windowSizeX = pointerController.borderWidget.localSize.x / 2f;
		windowSizeY = pointerController.borderWidget.localSize.y / 2f;
		leftBorderX = 0f - windowSizeX + pointerController.transform.localPosition.x;
		rightBorderX = windowSizeX + pointerController.transform.localPosition.x;
		topBorderY = 0f - windowSizeY + pointerController.transform.localPosition.y;
		bottomBorderY = windowSizeY + pointerController.transform.localPosition.y;
	}

	public void Remove()
	{
		if (base.gameObject.GetActive() && !removing)
		{
			removing = true;
			StartCoroutine(RemoveWithAnimation());
		}
	}

	private IEnumerator RemoveWithAnimation()
	{
		UIPanel pointerPanel = GetComponent<UIPanel>();
		HOTween.Init();
		HOTween.Kill(pointerPanel);
		HOTween.To(pointerPanel, 0.5f, new TweenParms().Prop("alpha", 0));
		yield return new WaitForSeconds(0.6f);
		Object.Destroy(base.gameObject);
	}

	private IEnumerator RemoveInTime(float time)
	{
		yield return new WaitForSeconds(time);
		Remove();
	}

	public void SetNewTargetVector(Vector3 newTargetVector)
	{
		targetVector = newTargetVector;
	}

	private void SetNewPointerPosition(Vector3 targetPosition)
	{
		newArrowObjPosition = mainCamera.WorldToScreenPoint(targetPosition);
		if (newArrowObjPosition.z < 0f)
		{
			newArrowObjPosition.x *= -1f;
			newArrowObjPosition.y *= -1f;
		}
		newArrowObjPosition -= screenCenter;
		newArrowObjPosition *= (float)uiRoot.activeHeight / (float)Screen.height;
		newArrowObjPosition.z = 0f;
		newRorarion = Quaternion.LookRotation(base.transform.position - arrowObject.position);
		newRorarion.y = 0f;
		newRorarion.x = 0f;
		if (arrowObject.localPosition.x > 0f)
		{
			correctionRot.z = arrowRot.z * -1f;
		}
		else
		{
			correctionRot.z = arrowRot.z;
		}
		betweenHorizontalBorders = false;
		betweenVerticalBorders = false;
		if (Mathf.Sign(newArrowObjPosition.x) >= 0f)
		{
			if (newArrowObjPosition.x < rightBorderX)
			{
				betweenHorizontalBorders = true;
			}
		}
		else if (newArrowObjPosition.x > leftBorderX)
		{
			betweenHorizontalBorders = true;
		}
		if (Mathf.Sign(newArrowObjPosition.y) >= 0f)
		{
			if (newArrowObjPosition.y < bottomBorderY)
			{
				betweenVerticalBorders = true;
			}
		}
		else if (newArrowObjPosition.y > topBorderY)
		{
			betweenVerticalBorders = true;
		}
		newArrowObjPosition.x = Mathf.Clamp(newArrowObjPosition.x, leftBorderX, rightBorderX);
		newArrowObjPosition.y = Mathf.Clamp(newArrowObjPosition.y, topBorderY, bottomBorderY);
		if (GameController.instance != null && GameController.instance.playerScript != null)
		{
			distance = Vector3.Distance(GameController.instance.playerScript.transform.position, targetPosition);
		}
		float num = PointerController.instance.lerpSpeed;
		if (betweenHorizontalBorders && betweenVerticalBorders)
		{
			iconObject.gameObject.SetActive(drawIconOnVisibleTarget);
			if (floatingObject != null)
			{
				floatingObject.gameObject.SetActive(true);
			}
			arrowObject.gameObject.SetActive(false);
			newIconObjPosition = newArrowObjPosition;
			newIconObjPosition.y += maxIconPosCorrection - Mathf.Clamp(distance, 0f, maxIconPosCorrection);
		}
		else
		{
			iconObject.gameObject.SetActive(drawArrow);
			arrowObject.gameObject.SetActive(drawArrow);
			num *= 2f;
			newIconObjPosition = newArrowObjPosition - newArrowObjPosition.normalized * arrowSize;
			if (floatingObject != null)
			{
				floatingObject.gameObject.SetActive(false);
			}
		}
		arrowSprite.transform.localRotation = correctionRot;
		arrowObject.localRotation = newRorarion;
		arrowObject.localPosition = new Vector3(newArrowObjPosition.x, newArrowObjPosition.y, 0f);
		if (showDistance)
		{
			distanceLabel.text = distance.ToString("f0") + " M";
		}
		iconObject.localPosition = Vector3.Lerp(iconObject.localPosition, newIconObjPosition, Time.deltaTime * num);
		if (floatingObject != null)
		{
			floatingObject.localPosition = newIconObjPosition;
		}
	}

	private void Update()
	{
		if ((bool)uiRoot)
		{
			if (pointToVector)
			{
				SetNewPointerPosition(targetVector);
				return;
			}
			if ((bool)target && target.gameObject.activeSelf)
			{
				SetNewPointerPosition(target.position);
				return;
			}
		}
		Remove();
	}
}
